\section{System Integration and Testing}
\label{sec:Testing}
In this section, the Integration Testing is described, starting by adding the controller to the encoder and PWM hardware in subsection~\ref{subsec:controllerImplementation} and ending by the Object Tracking implementation in subsection~\ref{subsec:ObjectTrackingImplementation}. Thereafter, the System Integration is treated.

\subsection{Controller Implementation}
\label{subsec:controllerImplementation}

In this subsection, the controller as described in section~\ref{sec:Controller} is implemented on the NIOS processor using a horizontal and vertical reference point of zero, which corresponds to the position halfway between the end-stops.

Because the controller is implemented for 100 Hz, a 100 Hz loop needs to be created in the NIOS processor. There are several ways to do this, namely interrupts or an infinite while loop. The possibility of interrupts were investigated, but this was not accurate enough for periodic interrupts every 10 ms, when also other tasks needed to be done during those 10 ms. Therefore, a while loop was used in combination with the high resolution timer running at 50 MHz. This provided both accurate and high resolution results. The code that implements this loop (already including the serial communication) can be found in listing \ref{lst:controllerloop}.

An experiment is performed on the JIWY setup where the initial horizontal and vertical positions are set well away from the middle, and a second experiment is performed where the initial positions are kept from the previous experiment, thus the initial values of the plant should ideally be halfway between the end-stops. In table~\ref{tab:homingpositions} the measurement results for both experiments are presented. From the table it can be seen that the end-stops are calculated properly, moreover, it can be seen in the last column that the total angle between the end-stops is exactly equal for the two experiments. Furthermore, it can be seen that the initial values for the second experiment were not exactly half the total angle. For the vertical measurement this is due to the gravity acting on the webcam module, for the horizontal measurement this is likely due to the discrete position measurement of the encoders (i.e. the accuracy of the encoders used is $\frac{1}{2000}\cdot2\pi\approx0.00314$).

\begin{table}[htbp!]
	\centering
	\caption{Measurement results for homing procedure}
	\label{tab:homingpositions}%
		\begin{tabular}{clccc}
		\hline
		\textbf{Experiment} & \textbf{Type} & \textbf{End-stop 1 (rad)} & \textbf{End-stop 2 (rad)} & \textbf{Total angle (rad)}\\
		\hline
		\multirow{2}[1]{*}{\textbf{1}}	& Horizontal	& -3.942695	& 1.533096 & 5.475791 \\
										&	Vertical	& -1.671326	& 0.383274 & 2.054600 \\
		\hline
		\multirow{2}[1]{*}{\textbf{2}}	& Horizontal	& -2.739466	& 2.736325 & 5.475791 \\
										&	Vertical	& -0.986459	& 1.068141 & 2.054600 \\
		\hline
		\end{tabular}%
\end{table}%


\subsection{Gumstix Communication Implementation}
Now that the controller is tested with the hardware, the serial communication as being described in section~\ref{sec:communication} is added to the system.
\\ \\
The Gumstix communication is tested using a sinusoid setpoint generator for both the x and y set-points. The  frequency of the sine wave is taken approximately one radian per second\footnote{The frequency is approximated because of the use of usleep, but its effects are neglectible because of the high processor speed.} and its amplitude is set to 0.5.
\\ \\
The JIWY results will be logged using the serial interface between the NIOS and the host computer in order to compare the real-time results with 20-sim simulation results. First it is tested whether the real-time simulation is affected by the logging facility or not. It appears that logging on 100Hz is not possible, since the serial buffer can not be empted fast enough, but that logging on 10Hz is possible. Measurement has shown that the spare time in one model calculation step is around 5ms from the 10ms available. In this spare time interval in 1 out of 10 loops, the model data is written to the serial interface. For the logging format, a CSV file format is being used such that the results can easily be loaded into 20-sim.
\\ \\
In figure~\ref{fig:communicationTestResults} the results of this experiment are shown together with the simulation results, where the simulation is excited with the sine sources as being measured. In the first 16 seconds, the homing procedure as being described above can be seen, as well as the 1 second waiting time after the homing.

\begin{figure}[h!]
	\centering
	\includegraphics[width=\textwidth]{images/controller_rt_logging_results.png} 
	\caption{Simulation results compared with real results}
	\label{fig:communicationTestResults}
\end{figure}

In the results, it can be seen that the sine sources on the Gumstix were started and stopped again during the homing procedure, which is not important because the setpoints are not evaluated during homing. Furthermore, the 1 second waiting time period in which the plant is controlled to zero position is visible. Interesting is the step-response at 17 seconds, where the simulation shows overshoot and the real setup does not, which is most likely due to higher friction in the JIWY setup then in the simulation. Visually, the plant behaves the same as the simulation, although closer inspection reveals that the real sine movement is somewhat less fluent. This is probably due to additional dynamics of the JIWY setup that are not being modelled, for example the belt between the gearing and the horizontal and vertical rotatable units.

		
\subsection{Object Tracking Implementation}
\label{subsec:ObjectTrackingImplementation}
The Object Tracking implementation can be tested now. The P-controller that is positioned in the Gumstix is initially fed with the gain as calculated in section \ref{subsec:diffangle}. Unfortunately, it turns out that the gain which has been working in the simulations, results in unstable behaviour in the real setup.
\\
Therefore, the Gumstix controller gain is set to a different value in practice, respectively $2.0\cdot10^{-3}$ and $2.5\cdot10^{-3}$ for the horizontal and vertical controller. Note that, although the simulation didn't provide in the exact values being used in practice, the simulation gave the correct order of magnitude.
\\
The fact that the simulation didn't completely match with the real setup is probably due to the incomplete system dynamics that are being described in the 20-sim model. For example, the backlash in the gears isn't taken into account, as well as the dynamics that are due to the beld transmission.

\subsection{System Integration}
Now that the system is fully functional, the system specifications can be validated against the actual system. Because of the loose specifications (since this is a design project), only qualitative results can be given.
\\
It can be seen in practice that the Jiwy is well able to follow any orange object, and that in a smooth fashion. Furthermore, the object tracking is considerably fast, in that even a thrown orange jacket can be tracked. In addition, only 4\% of the processor power in the Gumstix is used. This leaves plenty of processor power to be used for extensions to the algorithm.
